﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geodatabase;
using Akiha.Core;
using ESRI.ArcGIS.Geometry;


namespace ArcMap_AddIn_Tools.FeatureClass.UpdateDjzqbm
{
    public partial class UpdateDjzqbmForm : Form
    {
        private IMxDocument mxDocument = ArcMap.Document;
        private IFeatureClass zd_point_fc = null;
        private IFeatureClass djzq_fc = null;
        private string dzzqdm_field_name = "";
        private string djbm_field_name = "";

        public UpdateDjzqbmForm()
        {
            InitializeComponent();
        }

        private void UpdateDjzqbmForm_Load(object sender, EventArgs e)
        {
            LoadFeatureLayerToCombox();
        }

        private void LoadFeatureLayerToCombox()
        {
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, null);
            if (fcs.Count == 0)
                return;
            this.comboBox_ZdPointFC.BeginUpdate();
            this.comboBox_DjzqFC.BeginUpdate();
            for (int i = 0; i < fcs.Count; i++)
            {
                string name = fcs[i].Name;
                this.comboBox_ZdPointFC.Items.Add(name);
                this.comboBox_DjzqFC.Items.Add(name);
            }
            this.comboBox_ZdPointFC.EndUpdate();
            this.comboBox_DjzqFC.EndUpdate();
        }

        private void comboBox_ZdPointFC_SelectedIndexChanged(object sender, EventArgs e)
        {
            string zd_point_layer_name = this.comboBox_ZdPointFC.Text;
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, zd_point_layer_name);
            if (fcs.Count == 0)
                return;
            IFeatureLayer zd_point_layer = fcs[0];
            IFields fields = zd_point_layer.FeatureClass.Fields;
            zd_point_fc = zd_point_layer.FeatureClass;
            this.comboBox_ZdPointBM.BeginUpdate();
            for (int i = 0; i < fields.FieldCount; i++)
            {
                IField field = fields.get_Field(i);
                if (field.Editable == true)
                {
                    this.comboBox_ZdPointBM.Items.Add(field.Name);
                }
            }
            this.comboBox_ZdPointBM.EndUpdate();
        }

        private void comboBox_DjzqFC_SelectedIndexChanged(object sender, EventArgs e)
        {
            string djzq_layer_name = this.comboBox_DjzqFC.Text;
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, djzq_layer_name);

            if (fcs.Count == 0)
                return;
            IFeatureLayer djzq_layer = fcs[0];
            IFields fields = djzq_layer.FeatureClass.Fields;
            djzq_fc = djzq_layer.FeatureClass;

            this.comboBox_DjzqBM.BeginUpdate();
            for (int i = 0; i < fields.FieldCount; i++)
            {
                IField field = fields.get_Field(i);
                if (field.Editable == true)
                {
                    this.comboBox_DjzqBM.Items.Add(field.Name);
                }
            }
            this.comboBox_DjzqBM.EndUpdate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox_ZdPointFC.Text == "" || this.comboBox_ZdPointBM.Text == "" || this.comboBox_DjzqFC.Text == "" || this.comboBox_DjzqBM.Text == "")
            {
                return;
            }
            dzzqdm_field_name = this.comboBox_DjzqBM.Text;
            IQueryFilter QueryFilter = new QueryFilterClass();
            QueryFilter.SubFields = "*";
            QueryFilter.WhereClause = string.Format("");
            List<IFeature> djzqList = FeatureClassWrapper.GetFeaturesFromFeatureClass(djzq_fc, QueryFilter);
            for (int i = 0; i < djzqList.Count; i++)
            {
                IFeature djzqFeature = djzqList[i];
                IGeometry geo = djzqFeature.Shape;

                string djzq = FeatureWrapper.GetFieldValue(djzqFeature, dzzqdm_field_name).ToString();
                IEnvelope env = geo.Envelope;

                ISpatialFilter spatialFilter = new SpatialFilterClass();
                spatialFilter.Geometry = geo;
                spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                List<IFeature> listFeature = FeatureClassWrapper.GetFeaturesFromFeatureClass(zd_point_fc, spatialFilter);
                if (listFeature.Count == 0)
                {
                    continue;
                }
                UpdateFeature(zd_point_fc, listFeature, djzq, env, i + 1);
                string txt = string.Format("地籍子区进度：{0}/{1}", i + 1, djzqList.Count);
                this.label_DJZQ.Text = txt;
                Application.DoEvents();
            }
        }

        private void UpdateFeature(IFeatureClass FC, List<IFeature> listFeature, string djzq, IEnvelope env, int djzqindex)
        {
            djbm_field_name = this.comboBox_ZdPointBM.Text;
            List<IFeature> newListFeatue = new List<IFeature>();
            int rowindex = (int)Math.Round((env.YMax - env.YMin) / 25) + 1;
            string label = "";
            for (int i = 0; i < rowindex; i++)
            {
                double y_max = env.YMax - 25 * i;
                double y_min = env.YMax - 25 - 25 * i;
                List<IFeature> part_features = GetFeature(listFeature, y_min, y_max, i + 1);
                for (int j = 0; j < part_features.Count; j++)
                {
                    newListFeatue.Add(part_features[j]);
                    string txt = string.Format("宗地进度：{0}/{1}", newListFeatue.Count, listFeature.Count);
                    this.label_ZD.Text = txt;
                    Application.DoEvents();
                }
            }

            for (int i = 0; i < newListFeatue.Count; i++)
            {
                IFeature feature = newListFeatue[i];
                Dictionary<string, object> attribute = new Dictionary<string, object>();
                string bm = MathWrapper.FormatIntToStringAddZero((i + 1).ToString(), 5);
                string djqbm = djzq + "GB" + bm;
                attribute.Add(djbm_field_name, djqbm);
                FeatureClassWrapper.UpdateFeatureValues(FC, feature, attribute);
            }
        }




        private List<IFeature> GetFeature(List<IFeature> features, double y_min, double y_max, int rowindex)
        {
            List<IFeature> listFeature = new List<IFeature>();
            for (int i = 0; i < features.Count; i++)
            {
                IFeature pointFeature = features[i];
                IPoint point = pointFeature.Shape as IPoint;

                if (point.Y <= y_max && point.Y > y_min)
                {

                    if (listFeature.Count == 0)
                    {
                        listFeature.Add(pointFeature);
                    }
                    for (int j = 0; j < listFeature.Count; j++)
                    {
                        IFeature featue = listFeature[j];
                        IPoint featue_point = featue.Shape as IPoint;
                        if (point.X < featue_point.X)
                        {
                            if (!listFeature.Contains(pointFeature))
                            {
                                listFeature.Insert(j, pointFeature);
                                break;
                            }
                        }
                    }
                    if (!listFeature.Contains(pointFeature))
                    {
                        listFeature.Add(pointFeature);
                    }
                }
            }

            return listFeature;
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }


    }
}
